<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>登录注册</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
</head>
<style type="text/css">
    .aui-btn-block {
        margin-bottom: 10px;
    }
</style>
<body>
    <div class="aui-content">       
        <div class="aui-form">
            <div class="aui-input-row">
                <i class="aui-input-addon aui-iconfont aui-icon-people aui-text-info"></i>
                <input type="text" id="email" class="aui-input" placeholder="注册邮箱"/>
            </div>

            <div class="aui-input-row">
                <i class="aui-input-addon aui-iconfont aui-icon-lock aui-text-warning"></i>
                <input type="password" id="password" class="aui-input" placeholder="密码"/>
                <i class="aui-input-addon aui-iconfont aui-icon-attention aui-text-default" tapmode onclick="showPassword();" id="showpass-btn"></i>
            </div>
            
            <div class="aui-btn-row">
                <div class="aui-btn aui-btn-block aui-btn-info" tapmode onclick="login();">登录</div>
                <div class="aui-btn aui-btn-block aui-btn-default" tapmode onclick="toSetIndex()">没有账号？马上注册</div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../../script/api.js" ></script>
<script type="text/javascript">
    function toSetIndex(){
        api.execScript({
            name:'loginAndRegister_win',
            script:"setIndex(1)"
        })
    }
    apiready = function(){
        api.parseTapmode();
    };
    function login(){
        var email = $api.val($api.byId("email"));
        var password = $api.val($api.byId("password"));
        if(!email || !password){
            api.toast({
                msg: '请输入完整信息',
                duration:2000,
                location: 'top'
            });
            return;
        }
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '注册中',
            text: '请稍后',
            modal: false
        });
        api.ajax({
            url: 'http://www.maodoo.net/App/Index/login',
            method: 'post',
            timeout: 30,
            dataType: 'json',
            returnAll:false,
            data:{
                values:{
                    username:email,
                    password:password
                }
            }
        },function(ret,err){
            api.hideProgress();
            if (ret) {
                if(ret.code==200){
                    //登录成功
                    $api.clearStorage();
                    $api.setStorage('userid',''+ret.data.userid+'');
                    $api.setStorage('encrypt',''+ret.data.encrypt+'');
                    $api.setStorage('nickname',''+ret.data.nickname+'');
                    $api.setStorage('avatar',''+ret.data.avatar+'');
                    $api.setStorage('token',''+ret.data.token+'');
                    api.imageCache({
                        url: ret.data.avatar
                    },function(ret,err){
                    });
                    api.sendEvent({
                        name:'login',
                        extra:{
                            userid:ret.data.userid
                        }
                    })
                }else{
                    api.alert({
                        msg:''+ret.msg+''
                    });
                }
            }else {
                api.alert({
                    msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
                });
            }
        });
        
    }
    function showPassword(){
        $api.attr($api.byId('password'),'type','text');
        $api.removeCls($api.byId('showpass-btn'),'aui-icon-attention');
        $api.addCls($api.byId('showpass-btn'),'aui-icon-attentionfill');
        $api.attr($api.byId('showpass-btn'),'onclick','hidePassword();');
        api.parseTapmode();
    }
    function hidePassword(){
        $api.attr($api.byId('password'),'type','password');
        $api.removeCls($api.byId('showpass-btn'),'aui-icon-attentionfill');
        $api.addCls($api.byId('showpass-btn'),'aui-icon-attention');
        $api.attr($api.byId('showpass-btn'),'onclick','showPassword();');
        api.parseTapmode();
    }
</script>
</html>